package com.hrt.manage.sms.modual.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.hrt.base.BaseController;
import com.hrt.bean.Role;
import com.hrt.bean.SMSModual;
import com.hrt.bean.UserVO;
import com.hrt.manage.sms.modual.service.SMSModualService;
import com.hrt.util.PageUtils;

@Controller
@RequestMapping("manage/sms")
public class SMSModualController extends BaseController<SMSModual> {

	@Autowired
	private SMSModualService service;
	
	
	private SMSModual modual;

	@RequestMapping("modual/list")
	public ModelAndView list(HttpServletRequest request,
			HttpServletResponse response) {

		ModelAndView view = new ModelAndView();
		view.setViewName("manage/sms/modual/list");
		List<SMSModual> resultList = new ArrayList<SMSModual>();
		resultList = service.queryByPage(this.getPageIndex(),
				this.getPageSize());

		setTotalCount(service.totalCount());
		this.setList(resultList);
		this.setPageCount(this.computePageCount(this.getTotalCount(),
				this.getPageSize()));
		//
		page = this.getPage();
		// view.addObject("proTypes", getProTypes());
		view.addObject("pageResult", page);
		// view.addObject("productStatus", productStatus);
		// request.getSession().setAttribute("pageResult", page);
		return view;

	}

	@RequestMapping(value = "modual/queryByPage")
	@ResponseBody
	public Map<String, PageUtils<SMSModual>> queryByPage(
			@RequestParam(value = "pageIndex", required = false) Integer pageIndex,
			@RequestParam(value = "pageSize", required = false) Integer pageSize,
			HttpServletRequest request) {
		Map<String, PageUtils<SMSModual>> resultMap = new HashMap<String, PageUtils<SMSModual>>();

		if (pageSize == null || "".equals(pageSize)) {
			pageSize = 10;
		}
		if (pageIndex == null || "".equals(pageIndex)) {
			pageIndex = 1;
		}
		setPageIndex(pageIndex);

		setPageSize(pageSize);
		List<Map<String, Object>> conditions = new ArrayList<Map<String, Object>>();
		List<SMSModual> resultList = new ArrayList<SMSModual>();
		setTotalCount(this.service.totalCount());

		resultList = service.queryByPage(getPageIndex(), getPageSize());

		setList(resultList);

		this.setPageCount(this.computePageCount(this.getTotalCount(),
				this.getPageSize()));

		page = this.getPage();

		resultMap.put("pageResult", page);
		return resultMap;
	}

	@RequestMapping(value = "modual/add")
	@ResponseBody
	public Map<String, Object> add(HttpServletResponse response,
			HttpServletRequest request) {

		modual = new SMSModual();

		Map<String, Object> map = BaseController.transBean2Map(modual);

		return map;

	}

	@RequestMapping(value = "modual/save", method = RequestMethod.POST)
	public void save(@ModelAttribute("modualDetail") SMSModual modual,
			HttpServletRequest request, HttpServletResponse response) throws IOException {
		ModelAndView model = new ModelAndView();
		System.out.println("save");
		if(modual.getBussinessCode()!=null)
		{
			SMSModual	oldModual=service.getModualByBusinessCode(modual.getBussinessCode());
			if(oldModual!=null){
				PrintWriter pw=response.getWriter();
				pw.write("<script>alert('此业务类型模板已经存在');window.location='list.do'</script>");
				pw.flush();
				pw.close();
				return;
			}
		}
		UserVO manager = this.getOnlineManager(request, response);
		if (manager != null) {
			try {
				modual.setCreator(manager.getLoinName());

				modual.setCreateTime(this.service.getDate());

				this.service.saveModual(modual);

				response.sendRedirect("list.do");
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}
	
	@RequestMapping(value = "modual/delete", method = RequestMethod.GET)
	@ResponseBody
	public  Long[] delete(
			@RequestParam(value = "ids", required = true) String ids,
			HttpServletResponse response) throws IOException {
		Long[] idArr = null;
		ModelAndView view = new ModelAndView();

		if (ids != null) {
			if (ids.indexOf(",") != -1) {
				String[] idStrs = ids.split(",");
				idArr = new Long[idStrs.length];
				for (int idx = 0; idx < idStrs.length; idx++) {
					idArr[idx] = Long.parseLong(idStrs[idx]);
				}
			} else {
				idArr = new Long[] { Long.parseLong(ids) };
				
			}

		}
		
		this.service.deleteById(idArr);
		return idArr;

	}
	
	
	@RequestMapping(value = "modual/show")
	@ResponseBody
	public Map show(@RequestParam(value = "id", required = true) Long moId,
			HttpServletRequest request, HttpServletResponse response) {

		Map map = new HashMap();
		if (moId != null && !"".equals(moId)) {
			// Map resultMap=new HashMap<K, V>();
			
			this.modual = this.service.getModualById(moId);
			if (modual != null) {
				map = transBean2Map(modual);

			}

		}
		return map;

	}
	
	
	@RequestMapping(value = "modual/update", method = RequestMethod.POST)
	public void update(@ModelAttribute("modualDetail") SMSModual modual,
			HttpServletRequest request, HttpServletResponse response) {

		System.out.println("save");

		UserVO manager = this.getOnlineManager(request, response);
		
		SMSModual oldModual=service.getModualById(modual.getId());
		oldModual.setModualName(modual.getModualName());
		oldModual.setBussinessCode(modual.getBussinessCode());
		oldModual.setBussinessName(modual.getBussinessName());
		oldModual.setNote(modual.getNote());
		oldModual.setModualContent(modual.getModualContent());

		try {

			this.service.updateModual(oldModual);

			response.sendRedirect("list.do");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
